package com.lry.crawlers.listener;

import com.jnewbie.JHtml;
import com.lry.crawlers.comic.entity.ComicVariable;
import com.lry.crawlers.comic.pipeline.ComicPipeline;
import com.lry.crawlers.comic.processor.ComicPlusJProcessor;
import com.lry.crawlers.comic.service.ComicService;
import com.lry.crawlers.comic.service.ComicVariableService;
import com.lry.crawlers.config.service.ConfigService;
import com.lry.crawlers.configuration.RabbitMQConfig;
import com.lry.crawlers.information.entity.Information;
import com.lry.crawlers.information.service.InformationService;
import com.lry.crawlers.message.service.MessageService;
import com.lry.crawlers.util.ProcessVariable;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

@Component
public class ComicMQListener {
    @Autowired
    ComicPipeline comicPipeline;
    @Autowired
    ComicVariableService comicVariableService;
    @Autowired
    ComicService comicService;
    @Autowired
    InformationService informationService;
    @Autowired
    MessageService messageService;
    @Autowired
    ConfigService configService;



    //监听comic_queue队列
    @RabbitListener(queues = RabbitMQConfig.COMICEUE_NAME)
    public void ListenerComicQueue(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String name = new String(message.getBody());

        String[] split = name.split(",");
        if(informationService.getById(split[1])==null){
            channel.basicAck(deliveryTag, false);
            return;
        }
            Thread.sleep(3000);
        //MQ应答
        channel.basicAck(deliveryTag, false);
        informationService.updateInformation(new Information(split[1],"漫画","采集关键词："+split[0],"进行中",0,0,new Date(),null,20));
        try {
            comicVariableService.update();
            //搜索内容
            for (ComicVariable c : ProcessVariable.cVariable.values()) {
                //创建请求客户端
                JHtml jHtml = new JHtml();
                jHtml.setEncoding(c.getEncoding());
                //创建数据处理器
                ComicPlusJProcessor.initializeFilter();
                ComicPlusJProcessor comicJProcessor = new ComicPlusJProcessor(split[1],c, comicService,comicPipeline);
                //将客户端加入到处理器,设置开始url,设置启动线程
                comicJProcessor.setJHtml(jHtml)
                        .setGetMethod(JHtml.GET)
                        .setUrl(c.getSearchUrl().replace("{searchKey}",split[0]))
                        .start(configService.getOne(null).getComicThread());
            }
            ComicPlusJProcessor.allUrl = 0;
            informationService.updateInformation(new Information(split[1],new Date(),"完成",100));

        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw,true));
            String infomsg = sw.toString();
            channel.basicNack(deliveryTag, false, false);
            informationService.updateInformation(new Information(split[1],new Date(),"错误",10));
            messageService.setMessage(new com.lry.crawlers.message.entity.Message("漫画采集出现错误！title："+split[0]+"异常："+infomsg,new Date()));
        }}

    }






